package com.davidk.survey.client;

import com.google.gwt.user.client.ui.Composite;
import com.smartgwt.client.data.OperationBinding;
import com.smartgwt.client.data.RestDataSource;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.types.DSProtocol;
import com.smartgwt.client.widgets.grid.ListGrid;

public class SurveyListView extends Composite {

	public SurveyListView() {
		// DataSource dataSource = new DataSource();
		RestDataSource dataSource = new RestDataSource();
		dataSource.setDataFormat(DSDataFormat.JSON);
		// dataSource.setRecordXPath("/dataResponse");
		// dataSource.setDataURL("api/survey/list");
		// dataSource.setDataURL("testdata");

		OperationBinding fetch = new OperationBinding();
		fetch.setOperationType(DSOperationType.FETCH);
		fetch.setDataProtocol(DSProtocol.GETPARAMS);
		OperationBinding add = new OperationBinding();
		add.setOperationType(DSOperationType.ADD);
		add.setDataProtocol(DSProtocol.POSTMESSAGE);
		OperationBinding update = new OperationBinding();
		update.setOperationType(DSOperationType.UPDATE);
		update.setDataProtocol(DSProtocol.POSTMESSAGE);
		OperationBinding remove = new OperationBinding();
		remove.setOperationType(DSOperationType.REMOVE);
		remove.setDataProtocol(DSProtocol.POSTMESSAGE);
		dataSource.setOperationBindings(fetch, add, update, remove);

		DataSourceTextField idField = new DataSourceTextField("id", "ID");
		// idField.setPrimaryKey(true);
		// idField.setCanEdit(false);
		DataSourceTextField descField = new DataSourceTextField("description", "Description");

		dataSource.setFetchDataURL("api/survey/list");

		dataSource.setFields(idField, descField);

		ListGrid grid = new ListGrid();
		grid.setDataSource(dataSource);
		grid.setWidth100();
		grid.setHeight(500);
		grid.setAutoFetchData(true);
		initWidget(grid);
	}

}